package com.ruoyi.psi.base.warehouse.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.psi.base.warehouse.domain.*;
import com.ruoyi.psi.base.warehouse.mapper.WarehouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 仓库
 * 业务处理层
 */
@Service
public class WarehouseServiceImpl implements IWarehouseService {
    @Autowired
    private WarehouseMapper warehouseMapper;

    /**
     * 仓库查询
     *
     * @param cond
     * @return
     */
    @Override
    public List<WarehouseDto> selectWarehouseList(WarehouseCond cond) {
        List<WarehouseDto> warehouseDtos = warehouseMapper.selectWarehouseList(cond);
        for (WarehouseDto s : warehouseDtos) {
            if (StringUtils.isNotNull(warehouseMapper.selectWarehouseListListById(s.getWarehouseId()))) {
                s.setWarehouseLists(warehouseMapper.selectWarehouseListListById(s.getWarehouseId()));
            }
        }
        return warehouseDtos;
    }

    /**
     * 按编号查询
     *
     * @param warehouseId
     * @return
     */
    @Override
    public WarehouseDto selectWarehouseById(String warehouseId) {
        WarehouseDto warehouseDto = warehouseMapper.selectWarehouseById(warehouseId);
        if (StringUtils.isNotNull(warehouseMapper.selectWarehouseListListById(warehouseDto.getWarehouseId()))) {
            warehouseDto.setWarehouseLists(warehouseMapper.selectWarehouseListListById(warehouseDto.getWarehouseId()));
        }
        return warehouseDto;
    }

    /**
     * 仓库新增
     *
     * @param warehouse
     * @return
     */
    @Override
    public int insertWarehouse(WarehouseDomain warehouse) {
        if (StringUtils.isNotNull(warehouseMapper.selectWarehouseById(warehouse.getWarehouseId()))) {
            //存在
            return 0;
        }
        //不存在
        warehouseMapper.insertWarehouse(warehouse);
        List<WarehouseListDomain> warehouseLists = warehouse.getWarehouseLists();//仓库产品信息
        if (StringUtils.isNotNull(warehouseLists)) {
            for (int i = 0; i < warehouseLists.size(); i++) {
                warehouseMapper.insertWarehouseList(warehouseLists.get(i)); //仓库产品信息新增
            }
        }
        return 1;
    }

    /**
     * 仓库更新
     *
     * @param warehouse
     * @return
     */
    @Override
    public int updateWarehouse(WarehouseDomain warehouse) {
        if (!StringUtils.isNotNull(warehouseMapper.selectWarehouseById(warehouse.getWarehouseId()))) {
            //存在
            return 0;
        }
        //存在销售订单单据
        warehouseMapper.updateWarehouse(warehouse);//销售订单单据
        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(warehouse.getWarehouseId());
        List<WarehouseListDomain> warehouseLists = warehouse.getWarehouseLists();//销售订单产品信息
        for (int i = 0; i < warehouseLists.size(); i++) {
            WarehouseListDto warehouseListDto = warehouseMapper.selectWarehouseListById(warehouse.getWarehouseId(), warehouseLists.get(i).getProduceId());
            if (StringUtils.isNotNull(warehouseListDto)) {
                //存在销售订单详细
                for (int j = 0; j < warehouseListDtos.size(); j++) {
                    if (warehouseListDtos.get(j).getProduceId().equals(warehouseListDto.getProduceId())) {
                        warehouseListDtos.remove(j);//移除新旧都有的部分
                        break;
                    }
                }//更新
                warehouseMapper.updateWarehouseList(warehouseLists.get(i));
            } else {//新增
                warehouseMapper.insertWarehouseList(warehouseLists.get(i));
            }
        }
        for (int i = 0; i < warehouseListDtos.size(); i++) {
            //删除新旧重叠以外的部分
            warehouseMapper.deleteWarehouseListById(warehouseListDtos.get(i).getWarehouseId(), warehouseListDtos.get(i).getProduceId());
        }
        return 1;
    }

    /**
     * 更新仓库删除标志位
     *
     * @param warehouse
     * @return
     */
    @Override
    public int updateWarehouseDeleteFlag(WarehouseDomain warehouse) {
        return warehouseMapper.updateWarehouseDeleteFlag(warehouse);
    }

    /**
     * 仓库详细查询
     *
     * @param cond
     * @return
     */
    @Override
    public List<WarehouseListDto> selectWarehouseListList(WarehouseCond cond) {
        return warehouseMapper.selectWarehouseListList(cond);
    }

    /**
     * 查询仓库对应的所有产品详细
     *
     * @param warehouseId 仓库编号
     * @return
     */
    @Override
    public List<WarehouseListDto> selectWarehouseListListById(String warehouseId) {
        return warehouseMapper.selectWarehouseListListById(warehouseId);
    }

    /**
     * 查询仓库对应的指定产品详细
     *
     * @param warehouseId 仓库编号
     * @return
     */
    @Override
    public WarehouseListDto selectWarehouseListById(String warehouseId, String produceId) {
        return warehouseMapper.selectWarehouseListById(warehouseId, produceId);
    }

    /**
     * 仓库详细更新
     * @param warehouseList 仓库详细
     * @return
     */
    @Override
    public int updateWarehouseList(WarehouseListDomain warehouseList) {
        return warehouseMapper.updateWarehouseList(warehouseList);
    }

    /**
     * 查询产品的库存数量
     * @return
     */
    @Override
    public Map<String, Double> selectProductNumber(WarehouseCond cond) {
        Map<String, Double> productNumberMap = new HashMap<>();
        List<WarehouseDto> warehouseDtos = warehouseMapper.selectWarehouseList(cond);
        for (WarehouseDto s : warehouseDtos) {
            List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(s.getWarehouseId()); //子表
            if (StringUtils.isNotNull(warehouseListDtos)) {
                s.setWarehouseLists(warehouseListDtos);
                for (WarehouseListDto sl : warehouseListDtos) {
                    if (!productNumberMap.containsKey(sl.getProduceId())) { //不存在
                        productNumberMap.put(sl.getProduceId(), sl.getProductNumber()); //存入产品数
                    } else {
                        //更新产品数
                        Double productNumberOld = productNumberMap.get(sl.getProduceId());
                        productNumberMap.replace(sl.getProduceId(), productNumberOld + sl.getProductNumber());
                    }
                }
            }
        }
        return productNumberMap;
    }

//    /**
//     * 查询指定产品的库存数量
//     *
//     * @param produceId
//     * @return
//     */
//    @Override
//    public double selectProductNumber(String produceId) {
//        List<WarehouseDto> warehouseDtos = warehouseService.selectWarehouseList(new WarehouseCond());
//        double productNumber = 0.00;
//        for (WarehouseDto s : warehouseDtos) {
//            List<WarehouseListDto> warehouseLists = s.getWarehouseLists();
//            if (StringUtils.isNotNull(warehouseLists)) {
//                // 仓库的产品列表非空时
//                for (WarehouseListDto sl : warehouseLists) {
//                    // 如果有该产品，则累加产品数量
//                    if (sl.getProduceId().equals(produceId)) {
//                        productNumber += sl.getProductNumber();
//                    }
//                }
//            }
//        }
//        return productNumber;
//    }
}
